﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Grid
{
	public partial class Form1 : Form
	{
		private DataGridView dgCust = new DataGridView();
		private BindingSource bindingSource1 = new BindingSource();
		private SqlDataAdapter dataAdapter = new SqlDataAdapter();
		private Button reloadButton = new Button();
		private Button submitButton = new Button();
		private DataTable table = new DataTable("Recordings");
		private DataTable cust = new DataTable("Customers");

		[STAThreadAttribute()]
		public static void Main()
		{
			Application.Run(new Form1());
		}

		// Initialize the form. 
		public Form1()
		{
			dgCust.Dock = DockStyle.Fill;

			reloadButton.Text = "reload";
			submitButton.Text = "submit";
			reloadButton.Click += new System.EventHandler(reloadButton_Click);
			submitButton.Click += new System.EventHandler(submitButton_Click);

			FlowLayoutPanel panel = new FlowLayoutPanel();
			panel.Dock = DockStyle.Top;
			panel.AutoSize = true;
			panel.Controls.AddRange(new Control[] { reloadButton, submitButton });

			this.Controls.AddRange(new Control[] { dgCust, panel });
			this.Load += new System.EventHandler(Form1_Load);
			this.Text = "DataGridView databinding and updating demo";
			Size = new System.Drawing.Size(1200, 700);
		}

		int _count = 0;
		List<string> paths;

		List<string> imageTypes = new List<string> { "*.jpg", "*.png", "*.gif" };
		List<string> videoTypes = new List<string> { "*.mts", "*.avi", "*.mpg", "*.mp4" };

		private void Form1_Load(object sender, System.EventArgs e)
		{
			Stopwatch sw = new Stopwatch();

			sw.Restart();

			paths = new List<string>(10);

			_count = 0;
			string path = @"D:\Documents\Pictures";
			imageTypes.AsParallel().ForAll(ext => CountFiles(path, ext));
			//path = @"E:\Videos";
			//videoTypes.AsParallel().ForAll(ext => CountFiles(path, ext));
			int num1 = _count;
			long ts1 = sw.ElapsedMilliseconds;

			sw.Restart();
			_count = 0;
			path = @"D:\Pictures";
			foreach (string ext in imageTypes) CountFiles(path, ext);
			path = @"E:\Videos";
			foreach (string ext in videoTypes) CountFiles(path, ext);
			int num2 = _count;
			long ts2 = sw.ElapsedMilliseconds;

			MessageBox.Show(String.Format("{0} in {1} -- {2} in {3}", num1, ts1, num2, ts2));

			Environment.Exit(-1);
			//bindingSource1.DataSource = table;
			//dataGridView1.DataSource = bindingSource1;

			try
			{
				dgCust.Columns.Add("CustomerID", "Customer Id");
				dgCust.Columns.Add("CompanyName", "Company Name");
				dgCust.Columns.Add("ContactName", "Contact Name");
				dgCust.Columns.Add("ContactTitle", "Title");
			}
			catch(Exception ex )
			{
				MessageBox.Show(ex.Message);
			}

			GetData("select * from Customers");
		}

		public void CountFiles(string path, string ext)
		{
			//var files = imageTypes.AsParallel().SelectMany(type => Directory.EnumerateFiles(path, type));
			paths.AddRange(Directory.GetFiles(path, ext));
			try
			{
				foreach (string folder in Directory.GetDirectories(path))
				{
					CountFiles(folder, ext);
				}
			}
			catch { }
		}

		private void reloadButton_Click(object sender, System.EventArgs e)
		{
			// Reload the data from the database.
			//GetData(dataAdapter.SelectCommand.CommandText);
			dgCust.Rows[0].Cells["ContactName"].Value = "Pascual Starink";
		}

		private void submitButton_Click(object sender, System.EventArgs e)
		{
			//dataAdapter.Update((DataTable)bindingSource1.DataSource);
		}

		private void GetData(string selectCommand)
		{
			try
			{
				String connectionString = "Data Source=pasta\\sqlexpress;Initial Catalog=Northwind;Integrated Security=SSPI";
				dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
				SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
				dataAdapter.Fill(table);

				foreach( DataRow row in table.Rows )
				{
					dgCust.Rows.Add(row["CustomerID"], row["CompanyName"], row["ContactName"], row["ContactTitle"]);
				}
			}
			catch (SqlException ex)
			{
				MessageBox.Show(ex.Message);
			}
		}
	}
}
